//
//  SHLPlayerManager.m
//  SDSmartHome
//
//  Created by mac on 2018/11/28.
//  Copyright © 2018年 SHL. All rights reserved.
//

#import "SHLPlayerManager.h"

@interface SHLPlayerManager()<AVAudioPlayerDelegate>

// 类型
@property(nonatomic,copy)NSString *type;
// 播放路径
@property(nonatomic,copy)NSString *path;

@end

@implementation SHLPlayerManager
#pragma mark - 单例
+ (instancetype)manager{
    static SHLPlayerManager *manage;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manage = [SHLPlayerManager new];
    });
    return manage;
}
#pragma mark - 配置
- (void)setup{
    // 解决后台播放被中断
    [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
}

#pragma mark - 播放本地资源以类型区分 type 0:喇叭响 1:开关后备箱 2:开/锁车门 3:发动机启动 4:系统消息提示音5:报警提示音6:关后备箱
- (void)playMp3WithType:(NSString *)type{
    self.type = type;
    if ([type isEqualToString:@"4"]){
        // 收到系统消息默认提示音
        SystemSoundID soundID = 1012;
        AudioServicesPlaySystemSound(soundID);
        return;
    }
    if ([type isEqualToString:@"0"]) {
        self.path = [[NSBundle mainBundle]pathForResource:@"mingdi" ofType:@"mp3"];
    }else if ([type isEqualToString:@"1"]){
        self.path = [[NSBundle mainBundle]pathForResource:@"open_hbx" ofType:@"mp3"];
    }else if ([type isEqualToString:@"2"]){
        self.path = [[NSBundle mainBundle]pathForResource:@"suoche" ofType:@"mp3"];
    }else if ([type isEqualToString:@"3"]){
        self.path = [[NSBundle mainBundle]pathForResource:@"open_fdj_sound" ofType:@"mp3"];
    }else if ([type isEqualToString:@"5"]){
        self.path = [[NSBundle mainBundle]pathForResource:@"warningthree" ofType:@"mp3"];
    }else if ([type isEqualToString:@"6"]){
        self.path = [[NSBundle mainBundle]pathForResource:@"close_hbx" ofType:@"mp3"];
    }
    if (![self.audioPlayer isPlaying]) {
        // 播放
        [self play];
    }else{
        // 先暂停播放的内容
        [self pausePlayAll];
        // 继续播放
        [self play];
    }
    
}
#pragma mark - 播放系统声音的方法
- (void)playSystemSound:(uint32_t)sid{
    NSLog(@"sid = %d",sid);
    SystemSoundID soundID = sid;
    AudioServicesPlaySystemSound(soundID);
}
#pragma mark - 播放推送或者mqtt给予的声音文件的名字
- (void)playMp3WithSoundName:(NSString *)sname AndType:(NSString *)stype{
    self.sname = sname;
    // 指定音频文件的路径【名字|类型】
    self.path = [[NSBundle mainBundle] pathForResource:sname ofType:stype];
    if (!self.path) {
        return;
    }
    if (![self.audioPlayer isPlaying]) {
        // 播放
        [self play];
    }else{
        // 先暂停播放的内容
        [self pausePlayAll];
        // 继续播放
        [self play];
    }
}
#pragma mark - 播放
- (void)play{
    NSURL *url = [NSURL fileURLWithPath:self.path];
    if (_audioPlayer) {
        _audioPlayer = nil;
    }
    _audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    _audioPlayer.delegate = self;
    // 设置播放属性
    _audioPlayer.numberOfLoops = 0; // 不循环
    [_audioPlayer prepareToPlay]; // 准备播放，加载音频文件到缓存
    [_audioPlayer play];
    // 解决后台播放被中断
    //[self setup];
}
#pragma mark - 停止播放
- (void)pausePlayAll{
    if ([self.audioPlayer isPlaying]) {
        [self.audioPlayer pause];
    }
}
/**
 *  代理方法的实现
 */


- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    NSLog(@"播放完成");
    if (self.sname) {
        if ([self.sname isEqualToString:@"huanxing_mianban"]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"DidFinishPlayingNoti" object:nil];
        }
        self.sname = nil;
    }
}
/* if an error occurs while decoding it will be reported to the delegate. */
- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError * __nullable)error{
    if (error) {
        NSLog(@"声音播放出现错误：%@",error);
    }
}







@end
